<?php
  /*
  * Employee.php
  * Representation of an Employee profile
  */
class Employee extends AppModel
{
    var $name = 'Employee';
    var $primaryKey = 'employee_id';
    //validation rules
    var $validate = array(
	'employee_id'=> array(
		'rule'=>'numeric',
		'required'=>true
	),
	'name'=> array(
		'rule'=>'alphaNumeric',
		'required'=>true
	)
    );
		
    
  // Employees may belong to multiple groups and projects simultaneously
    public $hasAndBelongsToMany = array(
		    'Groups' => array(
			    'className'              => 'Group',
			    'joinTable'              => 'group_members',
			    'foreignKey'             => 'emp_id',
			    'associationForeignKey'  => 'group_id',
			    ),
		    'Projects' => array(
			    'className'              => 'Project',
			    'joinTable'              => 'project_members',
			    'foreignKey'             => 'employee_id',
			    'associationForeignKey'  => 'project_id',
			    ),
		    );

    public $hasMany = array(
      //An employee may create clock-in/out records
        'Hour' => array(
            'className'     => 'Hour',
            'conditions'    => array('Hour.employee_id' => '$this->employee_id'),
            'order'         => 'Hour.date DESC'
        ),
      //An Employee may request a change to a clock-in/out record
        'Request' => array(
            'className'     => 'Request',
            'conditions'    =>array('Request.employee_id' => '$this->employee_id'),
            'order'         => 'Request.date DESC'
        )
    );
    
    public function clock($clockIn, $empID, $projID)
    {
        return( $this->Hour->Save(array(
                'created_by' => $empID,
                'clock_in' => $clockIn,
                'employee_id' => $empID,
                'proj_id' => $projID
            )
        ));

    }
    public function currentlyIn($id){
	$recent = $this->Hour->Find('first',array('conditions'=>array('Hour.employee_id'=>$id),'order'=>array('Hour.date DESC')));
	if (!empty($recent['Hour']['clock_in'])){
		return true;
	}
	else{
		return false;
	}
    }
}
?>
